<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>qesp : hled.cpp Example File (examples/uartassistant/hled.cpp)</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="postheader" valign="center"><a href="index.html">Home</a> &middot; <a href="classes.html">All Classes</a> &middot; <a href="functions.html">All Functions</a> &middot;</td></tr></table><h1 class="title">hled.cpp Example File</h1>
<span class="small-subtitle">examples/uartassistant/hled.cpp</span>
<!-- $$$examples/uartassistant/hled.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">    <span class="preprocessor">#include &lt;QtCore/QTimer&gt;</span>

    <span class="preprocessor">#include &lt;QtGui/QColor&gt;</span>
    <span class="preprocessor">#include &lt;QtGui/QImage&gt;</span>
    <span class="preprocessor">#include &lt;QtGui/QPainter&gt;</span>
    <span class="preprocessor">#include &lt;QtGui/QPixmap&gt;</span>

    <span class="preprocessor">#include &quot;hled.h&quot;</span>

    <span class="keyword">class</span> HLed<span class="operator">::</span>Private
    {
    <span class="keyword">public</span>:
        Private()
            : darkerFactor(<span class="number">300</span>)<span class="operator">,</span> color(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>green)<span class="operator">,</span>
            isOn(<span class="keyword">true</span>)<span class="operator">,</span> offPixmap(<span class="number">0</span>)<span class="operator">,</span> onPixmap(<span class="number">0</span>)
        {
            offColor <span class="operator">=</span> color<span class="operator">.</span>darker(darkerFactor);
            timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qtimer.html">QTimer</a></span>;
            timer<span class="operator">-</span><span class="operator">&gt;</span>setInterval(<span class="number">250</span>);
        }

        <span class="operator">~</span>Private()
        {
            <span class="keyword">delete</span> offPixmap;
            <span class="keyword">delete</span> onPixmap;
            <span class="keyword">delete</span> timer;
        }

        <span class="type">int</span> darkerFactor;
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span> color;
        <span class="type">bool</span> isOn;

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span> offColor;
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span> <span class="operator">*</span>offPixmap;
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span> <span class="operator">*</span>onPixmap;

        HLed<span class="operator">::</span>State state;
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qtimer.html">QTimer</a></span> <span class="operator">*</span> timer;
    };

    HLed<span class="operator">::</span>HLed(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
        :<span class="type"><a href="http://qt.nokia.com/doc/4.8/qwidget.html">QWidget</a></span>(parent)<span class="operator">,</span> m_d(<span class="keyword">new</span> Private)
    {
        setColor(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>red);
        setState(Blink);
        connect(m_d<span class="operator">-</span><span class="operator">&gt;</span>timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(blinkToggle()));
    }

    HLed<span class="operator">::</span><span class="operator">~</span>HLed()
    {
        <span class="keyword">delete</span> m_d;
    }

    <span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span> HLed<span class="operator">::</span>color() <span class="keyword">const</span>
    {
        <span class="keyword">return</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>color;
    }

    HLed<span class="operator">::</span>State HLed<span class="operator">::</span>state() <span class="keyword">const</span>
    {
        <span class="keyword">return</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>state;
    }

    <span class="type">int</span> HLed<span class="operator">::</span>darkerFactor() <span class="keyword">const</span>
    {
        <span class="keyword">return</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>darkerFactor;
    }

    <span class="type">void</span> HLed<span class="operator">::</span>setColor(<span class="keyword">const</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span> <span class="operator">&amp;</span>color)
    {
        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>color <span class="operator">=</span><span class="operator">=</span> color)
            <span class="keyword">return</span>;

        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap) {
            <span class="keyword">delete</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap;
            m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap <span class="operator">=</span> <span class="number">0</span>;
        }

        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap) {
            <span class="keyword">delete</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap;
            m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap <span class="operator">=</span> <span class="number">0</span>;
        }

        m_d<span class="operator">-</span><span class="operator">&gt;</span>color <span class="operator">=</span> color;
        m_d<span class="operator">-</span><span class="operator">&gt;</span>offColor <span class="operator">=</span> color<span class="operator">.</span>darker(m_d<span class="operator">-</span><span class="operator">&gt;</span>darkerFactor);

        update();
    }

    <span class="type">void</span> HLed<span class="operator">::</span>setState(State state)
    {
        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">=</span><span class="operator">=</span> state)
            <span class="keyword">return</span>;

        <span class="keyword">if</span> (Blink <span class="operator">=</span><span class="operator">=</span> state) {
            m_d<span class="operator">-</span><span class="operator">&gt;</span>timer<span class="operator">-</span><span class="operator">&gt;</span>start();
        }
        <span class="keyword">else</span> {
            m_d<span class="operator">-</span><span class="operator">&gt;</span>timer<span class="operator">-</span><span class="operator">&gt;</span>stop();
            m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">=</span> (state<span class="operator">=</span><span class="operator">=</span>On) <span class="operator">?</span> <span class="keyword">true</span> : <span class="keyword">false</span>;
        }
        m_d<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">=</span> state;
        update();
    }

    <span class="type">void</span> HLed<span class="operator">::</span>setDarkerFactor(<span class="type">int</span> darkerFactor)
    {
        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>darkerFactor <span class="operator">=</span><span class="operator">=</span> darkerFactor)
            <span class="keyword">return</span>;

        m_d<span class="operator">-</span><span class="operator">&gt;</span>darkerFactor <span class="operator">=</span> darkerFactor;
        m_d<span class="operator">-</span><span class="operator">&gt;</span>offColor <span class="operator">=</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>color<span class="operator">.</span>darker(darkerFactor);

        update();
    }

    <span class="type"><a href="http://qt.nokia.com/doc/4.8/qsize.html">QSize</a></span> HLed<span class="operator">::</span>sizeHint() <span class="keyword">const</span>
    {
        <span class="keyword">return</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qsize.html">QSize</a></span>(<span class="number">20</span><span class="operator">,</span> <span class="number">20</span>);
    }

    <span class="type"><a href="http://qt.nokia.com/doc/4.8/qsize.html">QSize</a></span> HLed<span class="operator">::</span>minimumSizeHint() <span class="keyword">const</span>
    {
        <span class="keyword">return</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qsize.html">QSize</a></span>(<span class="number">16</span><span class="operator">,</span> <span class="number">16</span>);
    }

    <span class="type">void</span> HLed<span class="operator">::</span>toggle()
    {
        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">=</span><span class="operator">=</span> Blink) {
            <span class="keyword">return</span>;
        }

        setState(m_d<span class="operator">-</span><span class="operator">&gt;</span>state <span class="operator">=</span><span class="operator">=</span> On <span class="operator">?</span> Off : On);
        update();
    }

    <span class="type">void</span> HLed<span class="operator">::</span>turnOn(<span class="type">bool</span> on)
    {
        setState(on <span class="operator">?</span> On : Off);
    }

    <span class="type">void</span> HLed<span class="operator">::</span>turnOff(<span class="type">bool</span> off)
    {
        turnOn(<span class="operator">!</span>off);
    }

    <span class="type">void</span> HLed<span class="operator">::</span>blink()
    {
        setState(Blink);
    }

    <span class="type">void</span> HLed<span class="operator">::</span>blinkToggle()
    {
        m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">=</span> <span class="operator">!</span>m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn;
        update();
    }

    <span class="type">void</span> HLed<span class="operator">::</span>paintEvent(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qpaintevent.html">QPaintEvent</a></span><span class="operator">*</span> <span class="comment">/* event*/</span>)
    {
        <span class="keyword">if</span> (paintCachedPixmap())
        {
            <span class="keyword">return</span>;
        }

        <span class="type">int</span> width <span class="operator">=</span> ledWidth();
        <span class="keyword">const</span> <span class="type">int</span> scale <span class="operator">=</span> <span class="number">3</span>;
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span> <span class="operator">*</span>tmpPixmap <span class="operator">=</span> <span class="number">0</span>;

        width <span class="operator">*</span><span class="operator">=</span> scale;
        tmpPixmap <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span>(width <span class="operator">+</span> scale<span class="operator">*</span><span class="number">2</span><span class="operator">,</span> width <span class="operator">+</span> scale<span class="operator">*</span><span class="number">2</span>);
        tmpPixmap<span class="operator">-</span><span class="operator">&gt;</span>fill(palette()<span class="operator">.</span>color(backgroundRole()));
        tmpPixmap<span class="operator">-</span><span class="operator">&gt;</span>fill(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span>(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>transparent));

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpainter.html">QPainter</a></span> paint;
        paint<span class="operator">.</span>begin(tmpPixmap);
        paint<span class="operator">.</span>setRenderHint(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qcolor.html">QColor</a></span> color <span class="operator">=</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">?</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>color : m_d<span class="operator">-</span><span class="operator">&gt;</span>offColor;

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qbrush.html">QBrush</a></span> brush;
        brush<span class="operator">.</span>setStyle(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>SolidPattern);
        brush<span class="operator">.</span>setColor(color);

        paint<span class="operator">.</span>setBrush(brush);
        <span class="comment">// draw plain</span>
        paint<span class="operator">.</span>drawEllipse(scale<span class="operator">,</span> scale<span class="operator">,</span> width<span class="operator">-</span>scale<span class="operator">*</span><span class="number">2</span><span class="operator">,</span> width<span class="operator">-</span>scale<span class="operator">*</span><span class="number">2</span>);

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpen.html">QPen</a></span> pen;
        pen<span class="operator">.</span>setWidth(<span class="number">2</span> <span class="operator">*</span> scale);

        <span class="type">int</span> pos <span class="operator">=</span> width <span class="operator">/</span> <span class="number">5</span> <span class="operator">+</span> <span class="number">1</span>;
        <span class="type">int</span> lightWidth <span class="operator">=</span> width <span class="operator">*</span> <span class="number">2</span> <span class="operator">/</span> <span class="number">3</span>;
        <span class="type">int</span> lightQuote <span class="operator">=</span> <span class="number">130</span> <span class="operator">*</span> <span class="number">2</span> <span class="operator">/</span> (lightWidth <span class="operator">?</span> lightWidth : <span class="number">1</span>) <span class="operator">+</span> <span class="number">100</span>;

        <span class="comment">// draw bright spot</span>
        <span class="keyword">while</span> (lightWidth) {
            color <span class="operator">=</span> color<span class="operator">.</span>lighter(lightQuote);
            pen<span class="operator">.</span>setColor(color);
            paint<span class="operator">.</span>setPen(pen);
            paint<span class="operator">.</span>drawEllipse(pos<span class="operator">,</span> pos<span class="operator">,</span> lightWidth<span class="operator">,</span> lightWidth);
            lightWidth<span class="operator">-</span><span class="operator">-</span>;

            <span class="keyword">if</span> (<span class="operator">!</span>lightWidth) {
                <span class="keyword">break</span>;
            }

            paint<span class="operator">.</span>drawEllipse(pos<span class="operator">,</span> pos<span class="operator">,</span> lightWidth<span class="operator">,</span> lightWidth);
            lightWidth<span class="operator">-</span><span class="operator">-</span>;

            <span class="keyword">if</span> (<span class="operator">!</span>lightWidth) {
                <span class="keyword">break</span>;
            }

            paint<span class="operator">.</span>drawEllipse(pos<span class="operator">,</span> pos<span class="operator">,</span> lightWidth<span class="operator">,</span> lightWidth);
            pos<span class="operator">+</span><span class="operator">+</span>;
            lightWidth<span class="operator">-</span><span class="operator">-</span>;
        }

        pen<span class="operator">.</span>setWidth(<span class="number">2</span><span class="operator">*</span>scale <span class="operator">+</span> <span class="number">1</span>);
        brush<span class="operator">.</span>setStyle(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>NoBrush);
        paint<span class="operator">.</span>setBrush(brush);

        <span class="type">int</span> angle <span class="operator">=</span> <span class="operator">-</span><span class="number">720</span>;
        color <span class="operator">=</span> palette()<span class="operator">.</span>color(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qpalette.html">QPalette</a></span><span class="operator">::</span>Light);

        <span class="keyword">for</span> (<span class="type">int</span> arc<span class="operator">=</span><span class="number">120</span>; arc<span class="operator">&lt;</span><span class="number">2880</span>; arc<span class="operator">+</span><span class="operator">=</span><span class="number">240</span>) {
            pen<span class="operator">.</span>setColor(color);
            paint<span class="operator">.</span>setPen(pen);
            <span class="type">int</span> w <span class="operator">=</span> width <span class="operator">-</span> pen<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span> <span class="operator">-</span> scale <span class="operator">+</span> <span class="number">1</span>;
            paint<span class="operator">.</span>drawArc(pen<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span> pen<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span> w<span class="operator">,</span> w<span class="operator">,</span> angle<span class="operator">+</span>arc<span class="operator">,</span> <span class="number">240</span>);
            paint<span class="operator">.</span>drawArc(pen<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span> pen<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span> w<span class="operator">,</span> w<span class="operator">,</span> angle<span class="operator">-</span>arc<span class="operator">,</span> <span class="number">240</span>);
            color <span class="operator">=</span> color<span class="operator">.</span>darker(<span class="number">110</span>);
        }

        paint<span class="operator">.</span>end();

        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span> <span class="operator">*</span><span class="operator">&amp;</span>dest <span class="operator">=</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">?</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap : m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap);
        <span class="type"><a href="http://qt.nokia.com/doc/4.8/qimage.html">QImage</a></span> i <span class="operator">=</span> tmpPixmap<span class="operator">-</span><span class="operator">&gt;</span>toImage();
        width <span class="operator">/</span><span class="operator">=</span> scale;
        i <span class="operator">=</span> i<span class="operator">.</span>scaled(width<span class="operator">,</span> width<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>IgnoreAspectRatio<span class="operator">,</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qt.html">Qt</a></span><span class="operator">::</span>SmoothTransformation);
        <span class="keyword">delete</span> tmpPixmap;

        dest <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span>(<span class="type"><a href="http://qt.nokia.com/doc/4.8/qpixmap.html">QPixmap</a></span><span class="operator">::</span>fromImage(i));
        paint<span class="operator">.</span>begin(<span class="keyword">this</span>);
        paint<span class="operator">.</span>drawPixmap(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">*</span>dest);
        paint<span class="operator">.</span>end();
    }

    <span class="type">bool</span> HLed<span class="operator">::</span>paintCachedPixmap()
    {
        <span class="keyword">if</span> (m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">&amp;</span><span class="operator">&amp;</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap) {
            <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpainter.html">QPainter</a></span> paint(<span class="keyword">this</span>);
            paint<span class="operator">.</span>drawPixmap(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">*</span>m_d<span class="operator">-</span><span class="operator">&gt;</span>onPixmap);
            <span class="keyword">return</span> <span class="keyword">true</span>;
        }
        <span class="keyword">if</span> (<span class="operator">!</span>m_d<span class="operator">-</span><span class="operator">&gt;</span>isOn <span class="operator">&amp;</span><span class="operator">&amp;</span> m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap) {
            <span class="type"><a href="http://qt.nokia.com/doc/4.8/qpainter.html">QPainter</a></span> paint(<span class="keyword">this</span>);
            paint<span class="operator">.</span>drawPixmap(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">*</span>m_d<span class="operator">-</span><span class="operator">&gt;</span>offPixmap);
            <span class="keyword">return</span> <span class="keyword">true</span>;
        }
        <span class="keyword">return</span> <span class="keyword">false</span>;
    }

    <span class="type">int</span> HLed<span class="operator">::</span>ledWidth() <span class="keyword">const</span>
    {
        <span class="type">int</span> width <span class="operator">=</span> <a href="http://qt.nokia.com/doc/4.8/qtglobal.html#qMin">qMin</a>(<span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>width()<span class="operator">,</span> <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>height());
        width <span class="operator">-</span><span class="operator">=</span> <span class="number">2</span>;
        <span class="keyword">return</span> width <span class="operator">&gt;</span> <span class="number">0</span> <span class="operator">?</span> width : <span class="number">0</span>;
    }</pre>
</div>
<!-- @@@examples/uartassistant/hled.cpp -->
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2000-2012</td>
<td width="20%" align="center"><a href="http://code.google.com/p/qextserialport">QextSerialPort Project</a></td>
<td width="40%" align="right"><div align="right">QextSerialPort Manual</div></td>
</tr></table></div></address></body>
</html>
